/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package aptech.edu.ixj.impl;

import aptech.edu.ixj.entity.Answer;
import aptech.edu.ixj.entity.Question;
import aptech.edu.ixj.entity.Survey;
import aptech.edu.ixj.interfaces.ISurveyDao;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import nu.xom.Attribute;
import nu.xom.Builder;
import nu.xom.Document;
import nu.xom.Element;
import nu.xom.Node;
import nu.xom.Nodes;
import nu.xom.Serializer;
import nux.xom.xquery.XQueryUtil;

/**
 *
 * @author VAN
 */
public class SurveyDaoImpl implements ISurveyDao {

    public static final String SURVEY_XML = "D:\\Project\\thanhtv00011\\Survey\\Survey.xml";
    private Document doc = null;

    public List<Survey> readSurvey(String id) {
        List<Survey> surveys = new ArrayList<Survey>();
        List<String> questId = new ArrayList<String>();
        Survey survey;
        try {
            Document doc = new Builder().build(new File(SURVEY_XML));
            Nodes nodes = XQueryUtil.xquery(doc, "//Survey/categorys/category[@id='" + id + "']");
            for (int i = 0; i < nodes.size(); i++) {
                survey = new Survey();
                survey.setId(nodes.get(i).query("@id").get(0).getValue());
                survey.setContent(nodes.get(i).query("content").get(0).getValue());
                survey.setComment(nodes.get(i).query("comment").get(0).getValue());
                Nodes questions = nodes.get(i).query("question");

                for (int j = 0; j < questions.size(); j++) {
                    questId.add(questions.get(j).query("Content").get(0).getValue());
                }
                survey.setQuestionId(questId);
                surveys.add(survey);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return surveys;
    }



    public List<Survey> getListSurvey() {
        List<Survey> surveys = new ArrayList<Survey>();
        List<String> questId = new ArrayList<String>();
        Survey survey;
        try {
            Document doc = new Builder().build(new File(SURVEY_XML));
            Nodes nodes = XQueryUtil.xquery(doc, "//Survey/categorys/category");
            for (int i = 0; i < nodes.size(); i++) {
                survey = new Survey();
                survey.setId(nodes.get(i).query("@id").get(0).getValue());
                survey.setContent(nodes.get(i).query("content").get(0).getValue());
                survey.setComment(nodes.get(i).query("comment").get(0).getValue());
                Nodes questions = nodes.get(i).query("question");
                for (int j = 0; j < questions.size(); j++) {
                    questId.add(questions.get(j).query("Content").get(0).getValue());
                }
                survey.setQuestionId(questId);
                surveys.add(survey);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return surveys;
    }

    public boolean save() {
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(SURVEY_XML);
            Serializer ser = new Serializer(fos);
            ser.write(doc);
            ser.flush();
            return true;
        } catch (Exception e) {
            return false;
        }

    }

    public boolean removeSurvey(String surveyId) {
        try {
            Builder builder = new Builder();
            doc = builder.build(new File(SURVEY_XML));
            Node node = doc.query("//Survey/categorys/category").get(0);
            Node nodechild = doc.query("//Survey/categorys/category[@id='" + surveyId + "']").get(0);

            return true;
        } catch (Exception e) {

            return false;
        }

    }

    public void movie(Node node) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public static void main(String[] argm) {

        Survey s = new Survey();
        s.setComment("ss");
        s.setContent("sssss");
        s.setId("2");
        new SurveyDaoImpl().create(s);
        System.out.println("ok");

    }

    public boolean create(Survey survey) {
        try {
            Builder builder = new Builder();
            File f = new File(SURVEY_XML);
            f.createNewFile();
            doc = builder.build(f);
            Node nodes = doc.query("//Survey/categorys/category").get(0);
            Element content = new Element("content");
            content.appendChild(survey.getContent());
            Element question = new Element("questions");
            Element answer = new Element("answers");
            Element comment = new Element("comment");
            comment.appendChild(survey.getComment());
            Element category = new Element("category");
            Attribute id = new Attribute("id", survey.getId());
            category.addAttribute(id);
            category.appendChild(content);
            category.appendChild(question);
            category.appendChild(answer);
            category.appendChild(comment);
            nodes.getParent().fastInsertChild(category, nodes.getParent().getChildCount());
            save();
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    public void remove(org.w3c.dom.Node node) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public boolean updateQuetion(Question question) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public boolean updateAnswer(Answer answer) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public boolean disiableQuestion(String idQuestion) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
